﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Application.SiteLibraryManage;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
#endregion

namespace YunSpace.Web.Areas.SiteManage.Controllers
{
    public class SupplierController : ControllerBase
    {
        private SupplierApp supplierApp = new SupplierApp();


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string queryJson)
        {
            var data = new
            {
                rows = supplierApp.GetList(pagination, queryJson),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = supplierApp.GetForm(keyValue);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 提交表单
        /// </summary>
        /// <param name="supplierEntity">表单实体</param>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(SupplierEntity supplierEntity, string keyValue)
        {
            supplierApp.SubmitForm(supplierEntity, keyValue);

            return Success("操作成功。");
        }

        /// <summary>
        /// 删除表单
        /// </summary>
        /// <param name="keyValue">表单主键</param>
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            supplierApp.DeleteForm(keyValue);

            return Success("删除成功。");
        }
        /// <summary>
        /// 获取供应商名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetSupplierName(string F_Id)
        {
            var data = new
            {
                Sname = supplierApp.GetSupplierName(F_Id)
            };

            return Content(data.ToJson());
        }


        [HttpGet]
        public virtual ActionResult SelectSupplier()
        {
            return View();
        }


        public virtual ActionResult Detail()
        {
            return View();
        }

        public ActionResult GetDetailJson()
        {
            var operatorProvider = OperatorProvider.Provider.GetCurrent();
            var data = supplierApp.GetForm(operatorProvider.UserId);
            return Content(data.ToJson());
        }

        public ActionResult SubmitSupplierInfo(SupplierEntity supplier)
        {
            var operatorProvider = OperatorProvider.Provider.GetCurrent();
            supplier.F_Id = operatorProvider.UserId;
            supplierApp.UpdateForm(supplier);
            return Success("修改成功");
        }

        public ActionResult GetSupplierGridJson()
        {
            var operatorProvider = OperatorProvider.Provider.GetCurrent();
            var data = supplierApp.GetContactList(operatorProvider.UserId);
            return Content(data.ToJson());
        }

        public ActionResult Contact()
        {
            return View();
        }

        public ActionResult GetContactJson(string keyvalue)
        {
            var data = supplierApp.GetContact(keyvalue);
            return Content(data.ToJson());
        }

        [HandlerAjaxOnly]
        public ActionResult SubmitContactForm(SLContactEntity entity, string keyValue)
        {
            var operatorProvider = OperatorProvider.Provider.GetCurrent();
            entity.SupplierID = operatorProvider.UserId;
            if (supplierApp.SubmitContactForm(entity, keyValue))
                return Success("操作成功。");
            else
                return Error("存在相同的手机号");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult DeleteContactForm(string keyValue)
        {
            if (supplierApp.DelContactForm(keyValue))
                return Success("操作成功。");
            else
                return Error("必须保留一个场地管理员");
        }
    }
}


